package com.glw.sso.platform.common.util;

import org.springframework.util.StringUtils;

/**
 * @Author: guoliwei
 * @Date: 2021/11/14 13:20
 * @Version 1.0
 */
public class LoginContext {

    private static final ThreadLocal<LoginInfo> currentLoginUser = new ThreadLocal<>();

    public static LoginInfo getCurrentLoginUser() {
        return currentLoginUser.get();
    }

    public static String getCurrentLoginUsernameOrDefault(String def) {
        return StringUtils.isEmpty(currentLoginUser.get()) ? def : currentLoginUser.get().getUsername();
    }

    public static void checkin(LoginInfo loginInfo) {
        currentLoginUser.set(loginInfo);
    }

    public static void checkout() {
        currentLoginUser.remove();
    }

}

